<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of View_truck
 *
 * @author peerawatj20
 */
class view_truck extends MY_Controller {

    private $per_page;

    public function __construct() {
        parent::__construct();
        $this->load->model('truck_model');
        $this->load->model('repair/repair_model');
    }

    public function index() {
        $id = $this->input->get('id');
        $config['base_url'] = base_url() . "truck/view_truck?id=" . $id;
        $config['per_page'] = 10;
        $config['uri_segment'] = 3;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->repair_model->count_repair_by_truck_id($id);
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $this->per_page = $config['per_page'];


        $this->_set_form($id);
        $this->load->view('truck/view_truck', $this->data);
    }

    private function _set_form($id) {
        $utility = new Config_utility();
        $truck = $this->truck_model->get_truck_by_id($id);
        $this->data['lbl_license_plate'] = '<h5>' . $utility->check_str($truck[0]->license_plate_code) . '</h5>';
        $this->data['lbl_brand_name'] = '<h5>' . $utility->check_str($truck[0]->brand_name) . '</h5>';
        $this->data['lbl_model_name'] = '<h5>' . $utility->check_str($truck[0]->model_name) . '</h5>';
        $this->data['lbl_moter_code'] = '<h5>' . $utility->check_str($truck[0]->moter_code) . '</h5>';
        $this->data['lbl_mile_number'] = '<h5>' . $utility->check_str($truck[0]->mile_number) . '</h5>';
        $this->data['lbl_driver_name'] = '<h5>' . $truck[0]->prefix_name . $utility->check_str($truck[0]->first_name . " " . $truck[0]->last_name) . '</h5>';

        $img = $this->truck_model->get_truck_img_by_id($truck[0]->truck_id);
        if ($img != NULL) {
            for ($i = 0; $i < count($img); $i++) {
                $this->data['img_picture'][$i] = '<div class="show-image">
                 <img src="' . base_url() . 'uploads/trucks/' . $img[$i]->file_name . '"  style="width:355px;" />
                 </div>';
            }
        } else {
            $this->data['img_picture'][0] = '<img src="' . base_url() . 'images/driver/no_image.jpg" style="width:355px"  \>';
        }

        $this->_set_table($id);
    }

    private function _set_table($id) {
        $page = $this->input->get('per_page');
        $obj = $this->repair_model->get_repair_by_truck_id($id, $this->per_page, $page);
        $utility = new Config_utility();
        $count = 0;

        for ($i = 0; $i < count($obj); $i++) {

            $row = '<tr id="tr_row' . $i . '" style="font-size: 14px"> 
                    <td> 
                        ' . ($i + 1) . ' 
                    </td> 
                    <td> 
                         <div id="lblMainType' . $i . '" style="height:22px;">' . $obj[$i]->status_name . '</div> 
                    </td> 
                    <td> 
                         <div id="lblSubType' . $i . '" style="height:22px;">' . $utility->convert_date($obj[$i]->made_date_repair, $utility->DAY) . "  " .
                    $utility->month_to_string($utility->convert_date($obj[$i]->made_date_repair, $utility->MONTH)) . "  " .
                    $utility->convert_date($obj[$i]->made_date_repair, $utility->YEAR) . '</div> 
                    </td>
                    <td> 
                       <button type="button" class="btn btn-default btn-sm" onclick="location.href = ' . $this->_url_repair($obj[$i]->repair_id) . '">
                             <span class="glyphicon glyphicon-eye-open"></span>
                        </button>
                    </td> 
                </tr>';

            $this->data['row_table'][$i] = $row;
            $count++;
        }
        $this->data['row_total'] = $count;
    }

    private function _url_repair($id) {
        return "'" . base_url() . "repair/view_repair?id=" . $id . "&module=truck'";
    }

}
